﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SetUpdateSpeedCommand.cs" company="CET">
//   The Center of Educational Technology
// </copyright>
// <summary>
//   Defines the LoadPoolsCommand type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace F5.BigIp.LocalTrafficManager.UI.Commands
{
    using System;
    using System.Windows.Input;

    using F5.BigIp.LocalTrafficManager.UI.ViewModels;

    /// <summary>
    /// The load pools command.
    /// </summary>
    public class SetUpdateSpeed : ICommand
    {
        /// <summary>
        /// The view model.
        /// </summary>
        private readonly MainViewModel viewModel;

        /// <summary>
        /// Initializes a new instance of the <see cref="SetUpdateSpeed"/> class.
        /// </summary>
        /// <param name="viewModel">
        /// The view model.
        /// </param>
        public SetUpdateSpeed(MainViewModel viewModel)
        {
            this.viewModel = viewModel;
        }

        /// <summary>
        /// The can execute changed.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// The can execute.
        /// </summary>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool CanExecute(object parameter)
        {
            int updateSpeed;

            return this.viewModel.Timer != null &&
                   int.TryParse(parameter.ToString(), out updateSpeed);
        }

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        public void Execute(object parameter)
        {
            int updateSpeed;

            int.TryParse(parameter.ToString(), out updateSpeed);

            this.viewModel.Timer.Interval = new TimeSpan(0, 0, updateSpeed);
        }
    }
}
